package FileIO.ReaderWriter;

import FileIO.Student;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class useBufferedReader {
    public static void main(String[] args) throws IOException {
        List<Student> studentList = readStudents();
        for (Student s : studentList) {
            System.out.println(s);
        }
    }

    public static List<Student> readStudents() throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("src/main/data/students.txt"));
            List<Student> studentList = new ArrayList<>();
            String line = reader.readLine();
            while (line != null) {
                String[] fields = line.split(",");
                Student s = new Student();
                s.setName(fields[0]);
                s.setAge(Integer.parseInt(fields[1]));
                s.setScore(Double.parseDouble(fields[2]));
                studentList.add(s);
                line = reader.readLine();
            }
            return studentList;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
}
